import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Main extends Applet implements Runnable, MouseListener, MouseMotionListener
{		
	private Image dbImage;
	private Graphics dbg;
	Thread th;
	boolean running;
	
	public int mX;
	public int mY;
	
	public char screen;
	
	public StartScreen startScreen;
	public Game game;
		
	public void init()
	{	
		screen = 't';
		// i = Intro
		// t = Title
		// b = Board Select 
		// c = Character Select
		// g = Game
		
		startScreen = new StartScreen();
	
		Image cursorPic = getToolkit().getImage("coolCursor.gif");
		Cursor cursor = getToolkit().createCustomCursor(cursorPic, new Point(), "Steve");
		this.setCursor(cursor);
		
		addMouseMotionListener(this);
		addMouseListener(this);
		
		running = true;
	}
		
	public void start()
	{
		th = new Thread(this);
		th.start();
	}
	
	public void stop()
	{
		th.stop();
	}
	
	public void run()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		while (running)
		{			
			repaint();
			//System.out.println(screen);
			if(screen == 'g')
			{
				//game.updateGame();
			}
					
			try
			{
				Thread.sleep (50);
			}
			catch (InterruptedException ex)	{}
			
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}	
	}	
	
	public void paint(Graphics g)
	{
		//System.out.println("painting");
		if(screen == 't' || screen == 'b' || screen == 'c')
		{
			drawMenuScreen(g);
		}			
		else if(screen == 'g')
		{
			drawGame(g);
		}			
	}	
	
	public void drawMenuScreen(Graphics g)
	{
		Pic[] pics = startScreen.getPics();
			
		for(int i = 0; i < pics.length; i++)
		{
			Image img = getImage(getCodeBase(), pics[i].name);
			g.drawImage(img, pics[i].x, pics[i].y, this);
		}
	}
	
	public void drawGame(Graphics g)
	{
		//System.out.println("drawing board");
				
		Pic[] pics = game.getPics();
			
		for(int i = 0; i < pics.length-1; i++)
		{
			Image img = getImage(getCodeBase(), pics[i].name);
			g.drawImage(img, pics[i].x, pics[i].y, this);
		}
		
		Image img = game.getBoardImage();
		g.drawImage(img, 200, 65, this);
		
	}
	
	public void mouseMoved(MouseEvent e)
	{
		mX = e.getX();
		mY = e.getY();
		
		if(screen == 't' || screen == 'b' || screen == 'c')
		{
			startScreen.mouseMove(mX, mY);
		}
		else if(screen == 'g')
		{
			game.mouseMove(mX, mY);
		}
					
	  	e.consume();
	  	//repaint();
	}	
	
	public void mousePressed(MouseEvent e)
	{
		//System.out.println("click");
		mX = e.getX();
		mY = e.getY();
		
		if(screen == 'i')
		{
			
		}		
		else if(screen == 't' || screen == 'b' || screen == 'c')
		{
			screen = startScreen.click(mX, mY);
			
			if(screen == 'g')
			{
				game = new Game(startScreen.gameType);
			}
			else if(screen == 'q')
			{
				System.exit(0);
			}
		}		
		else if(screen == 'g')
		{
			//System.out.println("click - g");
			screen = game.click(mX,mY);	
		}
		
			
		e.consume();
	}
			
	public  void update (Graphics g)
	{
		if (dbImage == null)
		{
			dbImage = createImage (this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics ();
		}
		
		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
		
		dbg.setColor (getForeground());
		paint (dbg);
		
		g.drawImage (dbImage, 0, 0, this);	
	}
		
	public void mouseReleased(MouseEvent e){ e.consume(); }
	
	public void mouseExited(MouseEvent e){ e.consume(); }
	
	public void mouseEntered(MouseEvent e){ e.consume(); }
	
	public void mouseClicked(MouseEvent e){ e.consume(); }
	
	public void mouseDragged(MouseEvent e){ e.consume(); }
}